-- UISlimeInfo
-- Create by cheny3
-- 冈布奥一览界面

require "game/ui/form/comment/UIComment"
require "game/ui/form/talent/UISlimeDesc"

-- UISlimeInfo继承自Layer
UISlimeInfo = class("UISlimeInfo", function()
    return cc.Layer:create();
end);

function UISlimeInfo.create()
    return UISlimeInfo.new();
end

local MIN_GRID_COLUMN = 4;
local MAX_GRID_ROW    = 5;
local ITEM_NAME       = "item";

-- 阵营ID排序规则
local campSortRule = {2, 1, 3, 4};

-- 函数
local sortLictor;
local sortAllSlime;

-- 构造函数
function UISlimeInfo:ctor()
    -- 初始化
    self:setName("UISlimeInfo");
    local node = cc.CSLoader:createNode("layout/talent/SlimeInfo.csb");
    self:addChild(node);
    self.node = node;

    local CT = node:getChildByName("CT");
    local BT = node:getChildByName("BT");
    local scroll_view = findChildByName(CT, "scroll_view");

    self.CT = CT;
    self.BT = BT;
    self.scrollView = scroll_view;
    self.selectIndex = 1;

    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);

    -- 注册点击事件
    self:registerTouchEvent();

    -- 初始化冈布奥数据
    self:initData();

    -- 标签页
    self:redrawTabs();

    -- 初始化格子
    self:initSlimeGrids(self.node);

    -- 重绘
    self:redraw();

    -- 注册事件回调
    self:registerEventCallback();

    -- 适配
    self:resize();
end

-- 注册事件回调
function UISlimeInfo:registerEventCallback()
    EventMgr.register("UISlimeInfo", event.FOCUS_CHANGED, function(para)
        if para["get"] == "UISlimeInfo" then
            self:setVisible(true);
        end
    end);

    -- 关注评论数量更新
    EventMgr.register("UISlimeInfo", event.UPDATE_COMMENT_COUNT, function(para)
        self:checkCommentCount(para);
    end);

    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UISlimeInfo 界面析构清理");
            EventMgr.removeAll("UISlimeInfo");

            -- show bottom
            local uiTalentMain = UIMgr.getCurrentScene():getFormByName("UITalentMain");
            if uiTalentMain then
                uiTalentMain:showButtons();

                -- 显示冈布奥模型
                uiTalentMain:setSlimeVisible(true);
            end
        elseif ev == "enter" then
            -- hide bottom
            local uiTalentMain = UIMgr.getCurrentScene():getFormByName("UITalentMain");
            if uiTalentMain then
                uiTalentMain:hideButtons();

                -- 隐藏冈布奥模型
                uiTalentMain:setSlimeVisible(false);
            end
        end
    end);
end

-- 获取冈布奥数据
function UISlimeInfo:initData()
    -- 初始化
    self.data = {};
    self.campData = {};
    for i = 1, CampM.getCampsCount() do
        self.campData[i] = {};
    end

    -- 获取所有冈布奥class_id
    local list = PetM.getIllustratePets();

    for _, classId in pairs(list) do
        local pet = PetM.getMyPet(classId);
        local item = {};

        local departOrder = PetM.query(classId, "department_order");
        item.depart = departOrder[1];
        -- 获取数据
        if pet then
            -- 有宠物的话，取宠物的数据
            item.classId = classId;
            item.name = PetM.query(classId, "name");
            item.rank = pet.rank;
            item.noPet = 0;
        else
            -- 未获得宠物的话，去配置表的数据
            item.classId = classId;
            item.name = PetM.query(classId, "name");
            item.rank = PetM.query(classId, "init_rank");
            item.noPet = 1;
        end

        -- 阵营ID
        item.camp = CampM.getPetCamp(classId);

        if item.camp == -1 then
            -- 没配置，设成默认
            item.camp = 1;
        end

        -- 插入数据
        table.insert(self.data, item);
        table.insert(self.campData[item.camp], item);
    end

    -- 对宠物进行排序
    table.sort(self.data, sortAllSlime);

    -- 对阵营冈布奥进行排序
    for i = 1, CampM.getCampsCount() do
        table.sort(self.campData[i], sortLictor);
    end
end

-- 绘制标签页
function UISlimeInfo:redrawTabs()
    local tabNode = findChildByName(self.node, "CT/tab");

    -- 标签页数量（总览+阵营数）
    local tabNum = CampM.getCampsCount() + 1;

    -- 先移除旧的标签
    tabNode.rows = {};
    tabNode:removeAllChildren();

    -- 根据tab数据，生成新标签
    local widget = replaceNodeWithWidget(cc.CSLoader:createNode("layout/tab/tab.csb"));

    -- 置灰颜色
    local blendColor = TextStyleM.TEXT_COLOR_BLEND;
    local normalColor = TextStyleM.TEXT_COLOR_NORMAL;

    for i = 1, tabNum do
        local child = widget:clone();
        child:setName("tab" .. i);
        child.index = i;

        self:registerTabTouchEvent(child);

        -- 计算单元格坐标
        -- 这个坐标将是单元格的描点所在
        local x = (i - 1) * 68 + 55;
        local y = 75;

        child:setPositionX(x);
        child:setPositionY(y);

        local tabLight = findChildByName(child, "light");
        local tabImage = findChildByName(child, "tab");
        tabLight:setScaleX(0.7);
        tabImage:setScaleX(0.7);

        local tabText = findChildByName(child, "text");
        local tabIcon = findChildByName(child, "icon");

        tabText:setVisible(false);

        if i == 1 then
            tabLight:setVisible(true);
            tabImage:loadTexture(getTabIconPath("red"));
            -- TODO 暂代图标
            tabIcon:loadTexture(getSmallHeroIconPath("all"));
            tabIcon:setColor(normalColor);
            tabImage:setColor(normalColor);
        else
            tabLight:setVisible(false);
            tabIcon:loadTexture(getCampSmallIconPath(campSortRule[i - 1]));
            tabIcon:setColor(blendColor);
            tabImage:setColor(blendColor);
        end

        tabNode:addChild(child)
        table.insert(tabNode.rows, child)
    end

    for i = 1, #tabNode.rows do
        local tab = tabNode.rows[i];
        tab:setPositionY(tab:getPositionY() + 100);
    end

    performWithDelay(self, function() self:playTabEffect(); end, 0.2);
end

-- 页签动画效果
function UISlimeInfo:playTabEffect()
    local tabNode = findChildByName(self.node, "CT/tab");
    -- 带窗口动画播放结束后，页签从上面往下掉
    for i = 1, #tabNode.rows do
        local tab = tabNode.rows[i];
        local pos = cc.p(tab:getPosition());
        local delay = cc.DelayTime:create(0.15 * (i - 1));
        local moveDown = cc.MoveTo:create(0.15, cc.p(pos.x, pos.y - 100 - 5));
        local moveBack = cc.MoveTo:create(0.15, cc.p(pos.x, pos.y - 100));
        local action = cc.Sequence:create(delay, moveDown, moveBack);
        tab:runAction(action);
    end
end

-- 注册标签页点击回调
function UISlimeInfo:registerTabTouchEvent(node)
    -- 节点
    local tabImage = node:getChildByName("tab");
    local tabLight = node:getChildByName("light");
    local tabIcon = node:getChildByName("icon");
    local tabNode = findChildByName(self.node, "CT/tab");

    -- 置灰颜色
    local blendColor = TextStyleM.TEXT_COLOR_BLEND;
    local normalColor = TextStyleM.TEXT_COLOR_NORMAL;

    local function onClick(sender, eventType)
        -- 点击时的回调
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");
            -- 去除其它高亮
            for i = 1, #tabNode.rows do
                tabNode.rows[i]:getChildByName("light"):setVisible(false);
                tabNode.rows[i]:getChildByName("tab"):setColor(blendColor);
                tabNode.rows[i]:getChildByName("icon"):setColor(blendColor);
            end
            -- 设置本身高亮
            tabLight:setVisible(true);
            tabImage:setColor(normalColor);
            tabIcon:setColor(normalColor);

            -- 设置当前阵营ID
            if node.index == 1 then
                self.campId = nil;
            else
                self.campId = campSortRule[node.index - 1];
            end

            -- 重绘
            self.selectIndex = 1;
            self:initSlimeGrids(self.node);
            self:redraw();
            self.scrollView:jumpToTop();

            -- 切换标签页过快，刷新不过来，这里稍等会儿
            performWithDelay(self, function()
                if self.scrollView then
                    ScrollTool.scrollToTop(self.scrollView);
                end
            end, 0.01);
        end
    end

    tabImage:addTouchEventListener(onClick);
end

-- 初始化格子
function UISlimeInfo:initSlimeGrids(node)
    local w = DESIGN_WIDTH;
    local h = DESIGN_HEIGHT;

    -- 图标间距(横向)
    local wGap = 10;

    -- 图标间距(纵向)
    local hGap = 8;

    -- 图标宽度
    local iconWidth = 120;

    -- 图片高度
    local iconHeight = 120;

    local topHGap  = 10;
    local leftWGap = 5;
    local bottomHGap = 20;

    -- 冈布奥信息
    local slimeList;
    if self.campId then
        -- 阵营冈布奥
        slimeList = self.campData[self.campId];
    else
        -- 总览
        slimeList = self.data;
    end

    -- 冈布奥个数
    local slimeCount = #slimeList;

    ScrollTool.scrollToTop(self.scrollView);
    self.scrollView.rows = {};
    self.scrollView:removeAllChildren();

    local itemNode = cc.CSLoader:createNode("layout/hero/HeroItem.csb");
    itemNode:setScale(0.88);

    -- 获取icon的位置
    local iconNode = findChildByName(itemNode, "icon");
    self.iconPosY = iconNode:getPositionY();

    -- @param windowNode    界面节点
    -- @param scrollNode    容器节点
    -- @param itemNode      单元格的节点
    -- @param totalNum      单元格总数
    -- @param pageColNum    单页可以显示下的列数
    -- @param wGap          单元格间距(横向) 这里作为最小值使用
    -- @param hGap          单元格间距(纵向)
    -- @param iconWidth     单元格宽度
    -- @param iconHeight    单元格高度
    -- @param topHGap       上方留
    -- @param leftWGap      左右留白  这里作为最小值使用
    -- @param touchEvent    点击事件注册函数
    ScrollTool.initScrollGridsVertical(self, self.scrollView, itemNode, slimeCount, MIN_GRID_COLUMN, wGap, hGap, iconWidth, iconHeight, topHGap, bottomHGap, leftWGap, self.registerSlimeItemTouchEvent, self.InitSlimeItem);
end

-- 注册点击事件
function UISlimeInfo:registerTouchEvent()
    local node = self.node;

    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UISlimeInfo");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 评论系统
    local btnComment = findChildByName(self.node, "BT/choose_btn");
    local function onCommentClick(sender, eventType)
        if eventType ==  ccui.TouchEventType.ended then
            -- 打开评论界面
            AudioM.playFx("button_click");

            local data;
            if self.campId then
                data = self.campData[self.campId][self.selectIndex];
            else
                data = self.data[self.selectIndex];
            end
            local heroConfig = PetM.query(data.classId);

            -- 评论界面参数
            local uiData = {
                ["title"] = PetM.query(data.classId, "name"),
                ["type"] = "pet_" .. tostring(data.classId),
            };

            UIMgr.getCurrentScene():removeFormByName("UIComment");
            local uiComment = UIComment.create(uiData);
            UIMgr.getCurrentScene():addForm(uiComment);

            -- 图鉴界面隐藏，降低GL calls
            self:setVisible(false);
        end
    end
    btnComment:addTouchEventListener(onCommentClick);
    TextStyleM.setTextStyle(btnComment, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnComment:setTitleText("");

    local tipImg = findChildByName(self.CT, "tip_img");
    local function onTipClick(sender, eventType)
        if eventType == ccui.TouchEventType.began then
           tipImg:runAction(cc.ScaleTo:create(0.1, 0.97));
        elseif eventType == ccui.TouchEventType.canceled then
            tipImg:runAction(cc.ScaleTo:create(0.1, 1));
        elseif eventType == ccui.TouchEventType.ended then
            tipImg:runAction(cc.ScaleTo:create(0.1, 1));
            AudioM.playFx("button_click");
            self:showDesc();
        end
    end
    tipImg:addTouchEventListener(onTipClick);

    local btnTurn = findChildByName(self.node, "BT/turn_btn");
    local function onTurnClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 跳转到冈布奥主界面
            AudioM.playFx("button_click");
            local data;
            if self.campId then
                data = self.campData[self.campId][self.selectIndex];
            else
                data = self.data[self.selectIndex];
            end

            if not data then
                data = {};
            end

            local petId = data.classId;
            local pet = PetM.getMyPet(petId);

            if pet then
                self:setVisible(false);
                UIMgr.getCurrentScene():removeFormByName("UIHeroMain");

                -- 是否显示同阵营的冈布奥
                local isSameCamp = false;
                if self.campId then
                    isSameCamp = true;
                end

                local uiHeroMain = UIHeroMain.create(petId, isSameCamp);
                UIMgr.getCurrentScene():addForm(uiHeroMain);
                AudioM.playFx("ui_open1");
            else
                alert(getLocStr("havent_this_slime"));
            end

            -- 图鉴界面隐藏，降低GL calls
            self:setVisible(false);
        end
    end
    btnTurn:addTouchEventListener(onTurnClick);
    TextStyleM.setTextStyle(btnTurn, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnTurn:setTitleText(getLocStr("btn_text_turn"));

end

-- 重绘
function UISlimeInfo:redraw()
    -- 绘制标题
    local titleLabel = findChildByName(self.CT, "title_label");
    local subTitle = findChildByName(self.CT, "sub_title");
    local titleStr = "";
    if self.campId then
        titleStr = CampM.query(self.campId, "name");
    else
        titleStr = getLocStr("all_slime_title");
    end

    local tipImg = findChildByName(self.CT, "tip_img");

    -- 冈布奥信息
    local slimeList;
    if self.campId then
        -- 阵营冈布奥
        slimeList = self.campData[self.campId];

        tipImg:setVisible(false);
    else
        -- 总览
        slimeList = self.data;

        tipImg:setVisible(true);
    end
    local totalCount = #slimeList;

    local petsCount;
    if self.campId then
        petsCount = #CampM.getCampMyPet(self.campId);
    else
        petsCount = #table.keys(ME.user.pets);
    end
    -- 玩家的冈布奥个数
    local slimeCount = petsCount;

    local subStr = getLocStr("number_proportion");

    TextStyleM.setTitleStyle(titleLabel);
    TextStyleM.setTextStyle(subTitle, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_LIGHT_BLUE, true, -2);

    titleLabel:setString(titleStr);
    subTitle:setString(string.format(subStr, petsCount, totalCount));

    adjustTitlePos(titleLabel, subTitle, 0, titleStr, subText);

    TextStyleM.setShadowStyle(titleLabel, cc.size(0, -3));
    TextStyleM.setShadowStyle(subTitle, cc.size(0, -3));
    TextStyleM.setOutlineStyle(titleLabel, 1.5, TextStyleM.TEXT_OUTLINE_COLOR, true);
    TextStyleM.setOutlineStyle(subTitle, 1.5, TextStyleM.TEXT_OUTLINE_COLOR, true);

     -- 检查评论数量
    self:checkCommentCount();

    -- 绘制界面
    local itemCount;
    if self.campId then
        itemCount = #self.campData[self.campId];
    else
        itemCount = #self.data;
    end

    if not self.data or not self.campData or itemCount == 0 then
        cclog("获取冈布奥数据失败。");
        self.clearAllGrid();
    end

    self:checkTurnBtn();
end

function UISlimeInfo:InitSlimeItem(child, index)
    -- 图标纵向偏移
    local offsetY = -5;

    -- 设置选中效果
    local focusBg = child:getChildByName("focus");
    if self.selectIndex == index then
        focusBg:setVisible(true);
    else
        focusBg:setVisible(false);
    end

    --文本
    local str = "";
    -- 背景
    local bgImg = child:getChildByName("bg");
    local bgImgGray = child:getChildByName("gray_bg");
    -- 更改一下灰色背景图
    bgImgGray:loadTexture("images/ui/frame/hero_info_gray.png");

    -- icon
    local iconImage = child:getChildByName("icon");

    -- 头像的位置需要向下调整一些
    iconImage:setPositionY(self.iconPosY + offsetY);

    -- 星级
    local starImage = child:getChildByName("rank");
    -- "冒险中/进阶中"的tip
    local tipImage = child:getChildByName("tip");
    local tipText  = tipImage:getChildByName("text");

    -- 系别图标
    local departImg = child:getChildByName("depart_img");

    -- 默认全部隐藏
    bgImg:setVisible(false);
    bgImgGray:setVisible(false);
    iconImage:setVisible(false);
    starImage:setVisible(false);
    tipImage:setVisible(false);
    departImg:setVisible(false);

    local data;
    if self.campId then
        data = self.campData[self.campId][index];
    else
        data = self.data[index];
    end

    -- 可以升级的最大阶位
    local maxStarNum = PetM.getMaxUpgradeRank();
    local curStar, maxStar;
    if data then
        -- 有数据

        -- 设置宠物头像
        local iconIndex = PetM.getDefaultIcon(data.classId);
        iconImage:setVisible(true);

        if PetM.getMyPet(data.classId) then
            -- 玩家已获得该宠物
            -- 冈布奥图像
            iconImage:loadTexture(getHeroIconPath(iconIndex));
            bgImg:setVisible(true);

            -- 当前星阶/最大星阶数
            if data.rank >= maxStarNum then
                curStar = data.rank;
                maxStar = data.rank;
            else
                curStar = data.rank;
                maxStar = maxStarNum;
            end

            -- 设置星级
            starImage:setVisible(true);

            local starPath = "images/ui/symbol/star_little.png";

            -- 如果冈布奥已觉醒，使用橙色的星星
            if AwakeM.isCarved(data.classId) then
                starPath = "images/ui/symbol/star_little3.png";
            end
            local tStar = createStarLayer(curStar, starPath, "center", -0.5);
            if tStar then
                starImage:removeAllChildren();
                starImage:addChild(tStar);
            end

            -- 获取当前出战英雄
            local pet = ME.user:getActivePet();
            local classId = pet.classId;

            -- SLIMEC-1586 不再显示TIP
            TextStyleM.setTextStyle(tipText, TextStyleM.TEXT_SIZE_TINY, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true, -3);
            if data.classId == classId then
                -- 冒险中
                tipText:setString(getLocStr("pet_is_advanture"));
            elseif TrainingM.isUpgrading(data.classId) then
                -- 进阶中
                tipText:setString(getLocStr("slime_upgrading2"));
            elseif AwakeM.isCarving(data.classId) then
                -- 进阶中
                tipText:setString(getLocStr("slime_carving"));
            else
                tipImage:setVisible(false);
            end

            departImg:loadTexture(getCareerDepartmentIconPath(data.depart));
            departImg:setVisible(true);
        else
            -- 冈布奥轮廓图
            iconImage:loadTexture(getHeroSilhouetteIconPath(iconIndex));
            bgImgGray:setVisible(true);
            -- 隐藏掉不需要的内容
            bgImg:setVisible(false);
            starImage:setVisible(false);
            tipImage:setVisible(false);
        end
    end

    return child;
end

-- 刷新天赋
function UISlimeInfo:refreshTalent()
    local data;
    if self.campId then
        data = self.campData[self.campId][self.selectIndex];
    else
        data = self.data[self.selectIndex];
    end

    -- 隐藏
    local title = findChildByName(self.CT, "bg_3/caption_label");
    local explain = findChildByName(self.CT, "bg_3/explain_scroll/explain");
    title:setVisible(false);
    explain:setVisible(false);

    if not data then
        data = {};
    end

    -- 获取玩家宠物
    local pet = PetM.getMyPet(data.classId);

    if pet then
        -- 设置冈布奥天赋信息
        local skillId = pet:getTalentSkillId();
        local level = SkillM.query(skillId, "level");
        local name  = SkillM.query(skillId, "name");
        -- 棕色
        TextStyleM.setTextStyle(explain, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
        title:setString(string.format("%s Lv.%d", name, level));
        explain:setString(SkillM.getSkillDesc(skillId));
    else
        -- 设置标准模板
        -- 灰色
        TextStyleM.setTextStyle(explain, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GRAY);
        title:setString(PetM.query(data.classId, "name"));
        explain:setString(getLocStr("talent_activate_help_desc1"));
    end

    -- 徽章阵营图标
    local campId = CampM.getPetCamp(data.classId);
    local campIconImage = findChildByName(self.CT, "bg_3/camp_icon");
    campIconImage:loadTexture(getCampIconPath(campId));
    campIconImage:setVisible(true);
    campIconImage:setOpacity(200);

    TextStyleM.setTextStyle(title, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    title:setVisible(true);
    explain:setVisible(true);
end

-- 检查评论数量
function UISlimeInfo:checkCommentCount(para)
    local data;
    if self.campId then
        data = self.campData[self.campId][self.selectIndex];
    else
        data = self.data[self.selectIndex];
    end
    if not data then
        data = {};
    end

    -- 获取评论数量
    local commentType = "pet_" .. tostring(data.classId);
    -- 如果只是刷新界面的不请求
    if not para then
        Operation.cmd_get_comment_counts(commentType);
    end

    if para and para.commentType ~= commentType then
        return;
    end

    local count = CommentM.getCommentCount(commentType);
    local commentLabel = findChildByName(self.BT, "choose_btn/comment_label");

    local kerning = 0;
    if getLang() == "zh" or getLang == "tw" then
        kerning = 4;
    end

    TextStyleM.setTextStyle(commentLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true, kerning, nil, nil, nil, nil, true);
    commentLabel:setString(string.format(getLocStr("btn_text_comment_num"), count));
end

-- 对冈布奥进行排序  按阵营ID，ClassID排序
function sortLictor(item1, item2)
    if item1.camp ~= item2.camp then
        return campSortRule[item1.camp] < campSortRule[item2.camp];
    else
        return item1.classId < item2.classId;
    end
end

-- 对冈布奥总览页排序（拥有的排在前面）
function sortAllSlime(item1, item2)
    if item1.noPet ~= item2.noPet then
        return item1.noPet < item2.noPet;
    else
        if item1.camp ~= item2.camp then
            return campSortRule[item1.camp] < campSortRule[item2.camp];
        else
            return item1.classId < item2.classId;
        end
    end
end

-- 清空所有的格子
function UISlimeInfo:clearAllGrid()
    -- 格子总数
    local totalGridCount;
    if self.campId then
        totalGridCount = #self.campData[self.campId];
    else
        totalGridCount = #self.data;
    end

    for i = 1, totalGridCount do
        local grid = self.scrollView:getChildByName(ITEM_NAME .. i);
        if grid then
            grid:setVisible(false);
        end
    end
end

-- 注册格子点击事件
function UISlimeInfo:registerSlimeItemTouchEvent(node)
    local image = node:getChildByName("bg");
    local grayImage = node:getChildByName("gray_bg");
    local icon = node:getChildByName("icon");

    local function onClick(sender, eventType)
        -- 点击时的回调
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");

            if self.data[node.index] == nil then
                return;
            end

            local lastSelected = self.selectIndex;

            self.selectIndex = node.index;

            self:slimeSelected(lastSelected, node.index);
        end
    end

    image:addTouchEventListener(onClick);
    grayImage:addTouchEventListener(onClick);
end

-- 格子选中效果
function UISlimeInfo:slimeSelected(lastSelected, nowSelected)
    -- 清除上次选中效果
    local lastSel = self.scrollView:getChildByName(ITEM_NAME .. lastSelected);
    if lastSel then
        local focusBg = lastSel:getChildByName("focus");
        focusBg:setVisible(false);
    end

    -- 新选中的效果
    local nowSel = self.scrollView:getChildByName(ITEM_NAME .. nowSelected);
    if nowSel then
        local nowFocusBg = nowSel:getChildByName("focus");
        nowFocusBg:setVisible(true);
    end

    self:checkTurnBtn();
     -- 检查评论数量
    self:checkCommentCount();
end

-- 显示提示页面
function UISlimeInfo:showDesc()
    closeFormByName("UISlimeDesc");
    local uiForm = UISlimeDesc.create();
    UIMgr.getCurrentScene():addForm(uiForm);
end

function UISlimeInfo:checkTurnBtn()
    local turnBtn = findChildByName(self.BT, "turn_btn");

    local visi = turnBtn:isVisible();
    local data;
    if self.campId then
        data = self.campData[self.campId][self.selectIndex];
    else
        data = self.data[self.selectIndex];
    end
    if not data then
        data = {};
    end

    if data.noPet == 0 then
        turnBtn:setVisible(true);

        if not TrainingM.isUpgrading(data.classId) and
            not AwakeM.isCarving(data.classId) then
            turnBtn:setBright(true);
            turnBtn:setTouchEnabled(true);
        else
            -- 在训练室中,跳转按钮置灰
            turnBtn:setBright(false);
            turnBtn:setTouchEnabled(false);
        end
    else
        turnBtn:setVisible(false);
    end

    if  visi == false and data.noPet == 0 then
        turnBtn:setScale(0.5);
        local scaleUp = cc.ScaleTo:create(0.1, 1);
        local fadeIn = cc.FadeIn:create(0.1);
        turnBtn:runAction(cc.Sequence:create(cc.Spawn:create(scaleUp, fadeIn)));
    end
end

-- 适配
function UISlimeInfo:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");

    -- TOP顶部居中并缩放至合适大小
    --AlignM.alignToTopCenter(node, "TOP");
end